package com.smxy.mall.common;

import lombok.Data;

@Data
public class Response<T> {
    private static final String successCode = "0";
    private static final String errorCode = "1";
    private static final String successMsg = "请求正常";
    private static final String errorMsg = "请求失败";
    private String code;
    private String msg;
    private T data;

    public Response() {}

    public Response(String code, String msg){
        this.code = code;
        this.msg = msg;
    }

    public Response(String code,T data,String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    /**
     * 有返回数据
     * 返回固定msg code
     * @param data
     * @return
     */
    public static Response success(Object data) {
        return new Response(successCode,data,successMsg);
    }

    /**
     * 返回固定code  自定义msg
     * @param info
     * @return
     */
    public static Response fail(String info) {
        return new Response(errorCode,info);
    }

    /**
     * 有返回数据
     * 返回固定code  自定义msg
     * @param data
     * @param info
     * @return
     */
    public static Response success(Object data, String info) {
        return new Response(successCode,data,info);
    }

    /**
     * 无返回数据
     * 返回固定msg code
     * @return
     */
    public static Response success() {
        return new Response(successCode,successMsg);
    }

    /**
     * 无返回数据
     * 返回固定code  msg自定义
     * @param info
     * @return
     */
    public static Response success(String info) {
        return new Response(successCode,info);
    }

    /**
     * 返回固定code msg
     * @return
     */
    public static Response fail() {
        return new Response(errorCode,errorMsg);
    }


    /**
     * 返回自定义code msg
     * @param code
     * @param info
     * @return
     */
    public static Response fail(String code, String info) {
        return new Response(code,info);
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "Response{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
